package com.neusoft.neuiotms.dcom.converter.hn;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.hn.HnInspectionTaskItemValueDTO;
import com.neusoft.neuiotms.dcom.model.enums.HnInspectionItemDataType;
import com.neusoft.neuiotms.dcom.model.enums.HnInspectionTaskItemStatus;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionItem;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionTaskItemValue;

@Component
public class HnInspectionTaskItemValueConverter
        extends AbstractConverter<HnInspectionTaskItemValue, HnInspectionTaskItemValueDTO> {

    @Autowired
    private HnInspectionItemConverter hnInspectionItemConverter;

    @Override
    public HnInspectionTaskItemValue toModel(final HnInspectionTaskItemValueDTO dto) {
        final HnInspectionTaskItemValue model = new HnInspectionTaskItemValue();
        model.setStatus(dto.getStatus());
        model.setReason(dto.getReason());
        model.setValue(dto.getValue());
        final HnInspectionItem item = this.hnInspectionItemConverter.toModel(dto.getItem());
        if (StringUtils.isNotBlank(dto.getValue())) {
            if (item.getDataType().equals(HnInspectionItemDataType.select)) {
                if (dto.getValue().equals(DCOMConstants.HN_INSPECTION_ITEM_STATUS_NORMAL)) {
                    model.setStatus(HnInspectionTaskItemStatus.normal);
                } else {
                    model.setStatus(HnInspectionTaskItemStatus.alarm);
                }
            } else if (item.getDataType().equals(HnInspectionItemDataType.number)) {
                final ArrayList<String> candidate =
                        Lists.newArrayList(item.getCandidateValue().split(DCOMConstants.COMMA_SPILT));
                final List<Double> doubleCandidate = new ArrayList<>();
                candidate.stream().map(t -> {
                    final Double d = Double.valueOf(t);
                    return d;
                }).forEach(doubleCandidate::add);
                final List<Double> doubleSort = doubleCandidate.stream().sorted().collect(Collectors.toList());

                final Double min = doubleSort.get(0);
                final Double max = doubleSort.get(doubleSort.size() - 1);

                final Double value = Double.valueOf(dto.getValue());
                if ((value.doubleValue() >= min.doubleValue()) && (value.doubleValue() <= max.doubleValue())) {
                    model.setStatus(HnInspectionTaskItemStatus.normal);
                } else {
                    model.setStatus(HnInspectionTaskItemStatus.alarm);
                }
            }
        } else {
            model.setStatus(HnInspectionTaskItemStatus.uninspected);
        }
        model.setItem(item);
        return model;
    }

    @Override
    public HnInspectionTaskItemValueDTO toDTO(final HnInspectionTaskItemValue model, final boolean forListView) {
        final HnInspectionTaskItemValueDTO dto = new HnInspectionTaskItemValueDTO();
        dto.setStatus(model.getStatus());
        dto.setId(model.getId());
        dto.setReason(model.getReason());
        dto.setValue(model.getValue());
        dto.setItem(this.hnInspectionItemConverter.toDTO(model.getItem()));
        return dto;
    }

}
